admin=['天一','大空翼','暖哥','jski匪姐','柚子','大菠','赵嘻嘻','陈6岁🏂']
keys=['【时间】','【地点】']
gonggao='\n****Jski西南公告更新****\n单板教学、免费滑行视频分析@天一 @六岁'

def clearmsg():
    f = open('fyueren.txt','w+')
    f.write('')
    f.close()

def addmsg(s,name):
    file = open("fyueren.txt",'r')
    lines = file.readlines()
    file.close()
    f = open('fyueren.txt','a')
    if lines==[]:
        f.write(str(1)+':'+s+'【发布人】@%s '%(name)+'【参与人】@%s '%(name)+'\n')
    else:
        num=int((lines[-1])[0:1])
        f.write(str(num+1)+':'+s+'【发布人】@%s '%(name)+'【参与人】@%s '%(name)+'\n')
    f.close()

def showmsg():
    f=open('fyueren.txt', 'r')
    lines = f.readlines()
    strshow = '📣Jski约滑助手\n发布方法(j约+正确格式内容)\n删除方法(j删+序号)\n加入方法(j加+序号)\n鸽人方法(j鸽+序号)\n查看方法(直接发个 j)\n查看格式(直接发个 j约)\n'
    for line in lines:
        line = line.replace('【','\n【')
        strshow = strshow+'\n'+line
    f.close()
    strshow = strshow + gonggao
    return strshow

def delnum(delnum,name):
    file = open("fyueren.txt",'r')
    lines = file.readlines()
    n = 1
    with open("fyueren.txt",'w') as f:
        for line in lines:
            num = line[0:1]
            if int(n) == int(delnum):
                if '@%s '%name in line[line.rfind('【发布人】'):line.rfind('【参与人】')] or name in admin:
                    msg = '约滑信息 %s 已删除'%delnum
                    delnum = False
                else:
                    msg = '只有发布人和管理员可以删除'
                    newline=str(n)+line[1:]
                    f.write(newline)
                    n=n+1
            else:
                newline = str(n)+line[1:]
                f.write(newline) 
                n=n+1
        f.close()
        return msg

def addman(addnum,name):    
    file = open("fyueren.txt",'r')    
    lines = file.readlines()    
    n = 1    
    with open("fyueren.txt",'w') as f:
        for line in lines:            
            if int(n) == int(addnum):
                if '@%s '%name in line[line.rfind('【参与人】'):] :                    
                    newline = str(n) + line[1:]                    
                    f.write(newline) 
                    n = n + 1
                    msg = '@%s 你已经在 %s 了' % (name, addnum)
                else:
                    newline = str(n) + line[1:].replace('\n','') + '@%s \n'%name 
                    f.write(newline)
                    n = n + 1
                    msg = '@%s 加入 %s 成功' % (name, addnum)
            else:
                newline = str(n)+line[1:]
                f.write(newline)
                n=n+1
        f.close()        
        return msg

def delman(delnum,name):    
    file = open("fyueren.txt",'r')    
    lines = file.readlines()    
    n = 1    
    with open("fyueren.txt",'w') as f:
        for line in lines:            
            if int(n) == int(delnum):
                if '@%s '%name in line[line.rfind('【参与人】'):] or name in admin:
                    line1 = line[1:line.rfind('【参与人】') ]
                    line2 = line[line.rfind('【参与人】'): ].replace('@%s '%(name),'')
                    newline = str(n) + line1 + line2
                    f.write(newline)
                    n = n + 1
                    msg = '@%s 鸽了 %s' % (name, delnum)
                else:
                    newline=str(n)+line[1:] 
                    f.write(newline)
                    n=n+1
                    msg = '@%s 还没参与就想鸽？' % (name)
            else:
                newline = str(n)+line[1:]
                f.write(newline)
                n=n+1
        f.close()
        return msg

def yuerenhelp(act,name):
    act = act.replace('\n', '')
    act = act.replace(' ','')
    act = act.replace('***复制本条发布约滑信息***','')
    name=name.replace(' ','')
    for key in keys:
        flag = key in act
        flag = flag and flag
    if act=='j':
        msgshow=showmsg()
        return msgshow  
    elif 'j删' in act:
        act = act.replace('j删','')
        act=int(act)
        return delnum(act,name)
    elif 'j加' in act:
        act = act.replace('j加','') 
        act=int(act)
        return addman(act,name)
    elif 'j鸽' in act:
        act = act.replace('j鸽','') 
        act=int(act)
        return delman(act,name)
    elif 'j约' in act:
        if flag:
            act = act.replace('j约','') 
            addmsg(act,name)
            return showmsg()
        else:
            msg='j约\n【时间】\n【地点】\n***复制本条发布约滑信息***'
            return msg

